package com.bangdream.michelia.view.activity.base;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bangdream.michelia.R;
import com.bangdream.michelia.presenter.base.BasePresenter;

import io.reactivex.disposables.Disposable;


/**
 * 想要实现向右滑动删除Activity效果只需要继承SwipeBackActivity即可，如果当前页面含有ViewPager
 * 只需要调用SwipeBackLayout的setViewPager()方法即可
 *
 * @author xiaanming
 *
 */
public abstract class SwipeBackActivity<V,P extends BasePresenter<V>> extends BaseActivity<V,P> {
	protected SwipeBackLayout layout;

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
		super.onCreate(savedInstanceState, persistentState);
	}


	@SuppressLint("InflateParams") @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		layout = (SwipeBackLayout) LayoutInflater.from(this).inflate(
				R.layout.base, null);
		layout.attachToActivity(this);
	}

	public abstract int getLayout();
	public abstract void findId();
	public abstract void setListener();
	public abstract void logic();
	public abstract P createPresenter();


	@Override
	public void startActivity(Intent intent) {
		super.startActivity(intent);
		overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
	}

	@Override
	public void startActivityForResult(Intent intent, int requestCode) {
		super.startActivityForResult(intent, requestCode);
		overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
	}

	// Press the back button in mobile phone
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		overridePendingTransition(0, R.anim.base_slide_right_out);
	}


	public View noDataView;
	public View errorView;
	public View loadingView;
	public TextView errText;

	public void initEmptyView(RecyclerView recyclerView){
		noDataView = LayoutInflater.from(((ViewGroup) recyclerView.getParent()).getContext()).inflate(R.layout.empty_view, (ViewGroup) recyclerView.getParent(), false);
		errorView = LayoutInflater.from(((ViewGroup) recyclerView.getParent()).getContext()).inflate(R.layout.error_view, (ViewGroup) recyclerView.getParent(), false);
		loadingView = LayoutInflater.from(((ViewGroup) recyclerView.getParent()).getContext()).inflate(R.layout.loading_view, (ViewGroup) recyclerView.getParent(), false);
		errText = errorView.findViewById(R.id.errText);
	}

	protected Disposable disposable;

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (disposable != null && !disposable.isDisposed()) {
			disposable.dispose();
		}
	}

}
